package com.hong.order;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CurrentOrderTest {
    /**
     * 模拟多人同时下单
     *
     * @param args
     */
    public static void main(String[] args) {
        int currency = 50;
        // 循环屏障
        CyclicBarrier cb = new CyclicBarrier(currency);

        //OrderService orderService = new OrderService();
        // OrderServiceWithLock orderService = new OrderServiceWithLock();

        for (int i = 0; i < currency; i++) {
            new Thread(() -> {
                // 模拟集群环境,OrderServiceWithLock部署在不同的机器上
                OrderServiceWithLock orderService = new OrderServiceWithLock();
                System.out.println(Thread.currentThread().getName() + "----准备生成订单---");
                try {
                    // 等待所有人准备好了，一起出发
                    cb.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                orderService.createOrder();
            }).start();
        }
    }
}
